/***************************************************************************
 *            window-frame.c
 *
 *  Wed Feb 18 14:38:19 2009
 *  Copyright  2009  juanra
 *  <juanra@<host>>
 ****************************************************************************/

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 */


#include <stdlib.h>
#include <string.h>
 

#include "gettext-macros.h"
#include "window-frame.h"
#include "menu.h"
#include "file-treeview.h"
#include "callbacks.h"

#define MAX_DIR_LENGTH 200

char direction[MAX_DIR_LENGTH];
GtkWidget* file_view;
GtkWidget* entry;
 
GtkWidget*
create_window (void)
{
	GtkWidget *window;
	GtkWidget *vbox1;
	GtkWidget *frame;
	
	GtkWidget *top_menu;
	
	GtkWidget *vbox2;
	GtkWidget *hbox1;
	GtkWidget *hbox2;
	//GtkWidget* entry;
	GtkTreeSelection *selection;
	GtkWidget *label;
	GtkWidget *button;
	//GtkWidget* file_view;
	GtkWidget *swindow;
	
	GtkWidget *bar;
	


	
	GSList* view_options = NULL;
	
	GtkAccelGroup* accel_group;
	
	/*
	 Main window
	 */
	window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
	gtk_window_set_title( GTK_WINDOW( window ), "Sandia" );
	gtk_window_set_default_size( GTK_WINDOW( window ), 800, 480 );
	gtk_window_set_default_icon_from_file( "icon.gif", NULL );
	accel_group = gtk_accel_group_new();
	gtk_window_add_accel_group( GTK_WINDOW( window ), accel_group );
	g_signal_connect( G_OBJECT( window ), "destroy", G_CALLBACK( safe_quit ), NULL);
	
	/*
	 Main vbox
	 */
	vbox1 = gtk_vbox_new( FALSE, 2 );
	gtk_container_add( GTK_CONTAINER( window ), vbox1 );
	
	/*
	 Main menu
	 */
	top_menu = create_menu( accel_group, view_options );
	gtk_box_pack_start( GTK_BOX( vbox1 ), top_menu, FALSE, FALSE, 1 );

	
	
	/*
	 First frame
	 */
	frame = gtk_frame_new( "Host" );
	gtk_frame_set_shadow_type( GTK_FRAME( frame ), GTK_SHADOW_ETCHED_OUT );
	gtk_box_pack_start( GTK_BOX( vbox1 ), frame, TRUE, TRUE, 0 );
	
	/*
	 First inside vbox
	 */
	vbox2 = gtk_vbox_new( FALSE, 2 );
	gtk_container_add( GTK_CONTAINER( frame ), vbox2 );
	
	/*
	 Top button and direction entry
	 */
	hbox1 = gtk_hbox_new( FALSE, 10 );
	gtk_box_pack_start( GTK_BOX( vbox2 ), hbox1, FALSE, FALSE, 0 );
	button = gtk_button_new_from_stock( GTK_STOCK_GO_UP );
	gtk_box_pack_start( GTK_BOX( hbox1 ), button, FALSE, FALSE, 0 );
	hbox2 = gtk_hbox_new( FALSE, 0 );
	gtk_box_pack_start( GTK_BOX( hbox1 ), hbox2, TRUE, TRUE, 0 );
	label = gtk_label_new( N_("Direction:") );
	gtk_box_pack_start( GTK_BOX( hbox2 ), label, FALSE, FALSE, 0 );
	
	g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( on_up_button_clicked ), NULL );
	
	entry = gtk_entry_new();

	if ( getenv( "USER" ) == NULL )
		printf( "Error....\n" );
	
	if ( strcmp( "root", getenv( "USER" ) ) == 0 )
		strcpy( direction, "/root/" );
	else
	{
		strcpy( direction, "/home/" );
		strcat( direction, getenv( "USER" ) );
		strcat( direction, "/" );
	}
	gtk_entry_set_text( GTK_ENTRY( entry ), direction );
	gtk_box_pack_start( GTK_BOX( hbox2 ), entry, TRUE, TRUE, 5 );
	
	g_signal_connect( G_OBJECT( entry ), "activate", G_CALLBACK( on_entry_enter ), NULL );
	
	/*
	 Scrolled window for file/music view
	 */
	swindow = gtk_scrolled_window_new( NULL, NULL );
	gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( swindow ), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
	gtk_box_pack_end( GTK_BOX( vbox2 ), swindow, TRUE, TRUE, 0 );
	
	/*
	 File/music view
	 */
	file_view = create_initial_mv();
	file_view = fill_mv_from_direction();
	gtk_container_add( GTK_CONTAINER( swindow ), file_view );
	
	selection = gtk_tree_view_get_selection( GTK_TREE_VIEW( file_view ) );
	gtk_tree_selection_set_mode( selection, GTK_SELECTION_BROWSE );
	
	g_signal_connect( G_OBJECT( file_view ), "row-activated", G_CALLBACK( on_file_view_row_activated ), NULL );
	
	
	
	/*
	 Middle buttons
	 */
	hbox1 = gtk_hbox_new( FALSE, 0 );
	gtk_box_pack_start( GTK_BOX( vbox1 ), hbox1, FALSE, FALSE, 0 );
	
	button = gtk_button_new_from_stock( GTK_STOCK_GO_UP );
	gtk_box_pack_start( GTK_BOX( hbox1 ), button, TRUE, FALSE, 0 );
	button = gtk_button_new_from_stock( GTK_STOCK_GO_DOWN );
	gtk_box_pack_start( GTK_BOX( hbox1 ), button, TRUE, FALSE, 0 );
	
	
	
	/*
	 DAP frame
	 */
	frame = gtk_frame_new( "DAP" );
	gtk_frame_set_shadow_type( GTK_FRAME( frame ), GTK_SHADOW_ETCHED_OUT );
	gtk_box_pack_start( GTK_BOX( vbox1 ), frame, TRUE, TRUE, 0 );
	
	
	
	/*
	 Progress bar
	 */
	bar = gtk_progress_bar_new();
	gtk_progress_bar_set_text( GTK_PROGRESS_BAR( bar ), N_("Empty queue") );
	gtk_box_pack_start( GTK_BOX( vbox1 ), bar, FALSE, FALSE, 0 );
	

	
	
	
	return window;
}